/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.eventos.converters;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.cip.esymons.eventos.be.TipoIngresoEventos;
import org.cip.esymons.eventos.beans.TipoIngresoController;

/**
 *
 * @author oscar
 */
public class TipoIngresoConverter implements Converter{

   @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }

        Integer id = new Integer(value);
        TipoIngresoController controller = (TipoIngresoController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "TipoIngresoController");
        try {
            return controller.getDelegate().buscarTipoIngreso(id);
        } catch (Exception ex) {
            Logger.getLogger(EventoConverters.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof TipoIngresoEventos) {
            TipoIngresoEventos o = (TipoIngresoEventos) value;
            return o.getTpiCodigo() == null ? "" : o.getTpiCodigo().toString();
        } else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: org.cip.esymons.eventos.be");
        }
    }

}